package controllers;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import beans.LogSearchBeanRemote;

import dto.LogTypesDTO;
import dto.ServerLogEntryDTO;

/**
 * 
 * @author Zsombor
 *
 */
@ManagedBean
public class TypesDTOController {

	@EJB
	LogSearchBeanRemote cb;
	
	@ManagedProperty(value= "#{types}")
	private LogTypesDTO types;
	private Boolean show=false;
	private Boolean errshow=false;
	private String query;
	private List<ServerLogEntryDTO> result;
	private boolean sortAscending = false; 
	private boolean showSenderIcon = false;
	private boolean showIdIcon = false;
	
	public Boolean getShow() {
		return show;
	}

	public void setShow(Boolean show) {
		this.show = show;
	}

	public List<ServerLogEntryDTO> getResult() {
		return result;
	}

	public void setResult(List<ServerLogEntryDTO> result) {
		this.result = result;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public LogTypesDTO getTypes() {
		return types;
	}

	public void setTypes(LogTypesDTO types) {
		this.types = types;
	}
	
	public Boolean getErrshow() {
		return errshow;
	}
	

	public boolean isSortAscending() {
		return sortAscending;
	}

	public void setSortAscending(boolean sortAscending) {
		this.sortAscending = sortAscending;
	}


	public void setErrshow(Boolean errshow) {
		this.errshow = errshow;
	}
	
	public String sortBySender() {
		 
		   if(sortAscending){
	 
			//ascending order
			Collections.sort(result, new Comparator<ServerLogEntryDTO>() {
	 
				@Override
				public int compare(ServerLogEntryDTO slo1, ServerLogEntryDTO slo2) {
	 
					return slo1.getSender().compareTo(slo2.getSender());
	 
				}
	 
			});
			showSenderIcon = true;
			showIdIcon = false;
			sortAscending = false;
	 
		   }else{
	 
			//descending order
			Collections.sort(result, new Comparator<ServerLogEntryDTO>() {
	 
				@Override
				public int compare(ServerLogEntryDTO slo1, ServerLogEntryDTO slo2) {
	 
					return slo2.getSender().compareTo(slo1.getSender());
	 
				}
	 
			});
			showIdIcon =false;
			showSenderIcon = true;
			sortAscending = true;
		   }
	 
		   return null;
		}
	
	public String sortById() {
		 
		   if(sortAscending){
	 
			//ascending order
			Collections.sort(result, new Comparator<ServerLogEntryDTO>() {
	 
				@Override
				public int compare(ServerLogEntryDTO slo1, ServerLogEntryDTO slo2) {
	 
					return slo1.getId().compareTo(slo2.getId());
	 
				}
	 
			});
			showIdIcon = true;
			showSenderIcon = false;
			sortAscending = false;
	 
		   }else{
	 
			//descending order
			Collections.sort(result, new Comparator<ServerLogEntryDTO>() {
	 
				@Override
				public int compare(ServerLogEntryDTO slo1, ServerLogEntryDTO slo2) {
	 
					return slo2.getId().compareTo(slo1.getId());
	 
				}
	 
			});
			showIdIcon=true;
			showSenderIcon = false;
			sortAscending = true;
		   }
	 
		   return null;
		}


	public String print(){
		result = cb.queryEntries(types);
		;
		if(result.isEmpty()){
			show = false;
			errshow = true;
			} else {
				show = true;
				errshow = false;
			}
		
		return query;
	}
	
	public String getAll(){
		result = cb.queryAllEntries();
		;
		if(result.isEmpty()){
			show = false;
			errshow = true;
			} else {
				show = true;
				errshow = false;
			}
		
		return query;
	}

	public boolean isShowSenderIcon() {
		return showSenderIcon;
	}

	public void setShowSenderIcon(boolean showSenderIcon) {
		this.showSenderIcon = showSenderIcon;
	}

	public boolean isShowIdIcon() {
		return showIdIcon;
	}

	public void setShowIdIcon(boolean showIdIcon) {
		this.showIdIcon = showIdIcon;
	}

}